package com.changfang.gold_man.controller;

import com.changfang.gold_man.common.ImageUtil;
import com.changfang.gold_man.common.ResCode;
import com.changfang.gold_man.common.ResultVO;
import com.changfang.gold_man.service.Imp.CarServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/car")
@CrossOrigin(value = "*",maxAge = 1800,allowedHeaders = "*")

public class CarController {
    @Autowired
    private CarServiceImpl carServiceImpl;
    //查询所有车辆
    @GetMapping("/all")
    public ResultVO getAllCars() {

        return new ResultVO(ResCode.SUCCESS,carServiceImpl.findAll());
    }
    //注册车辆
    @PostMapping("/register")
    public ResultVO register(HttpServletRequest request, @RequestParam("file") MultipartFile file) throws IOException {
        if(!file.isEmpty()){
            String user_id = request.getParameter("user_id");
            String car_address = request.getParameter("car_address");
            String car_type = request.getParameter("car_type");
            String car_gold = request.getParameter("car_gold");
            String url = ImageUtil.fileUpload(file);
            System.out.println(url);
            carServiceImpl.register( user_id,car_type,car_address,url,car_gold);
            return new ResultVO(ResCode.SUCCESS,"添加记录成功");
        }
        return new ResultVO(ResCode.ERROR,"添加记录失败");
    }
    //通过用户id查找车辆
    @GetMapping("/findByUid")
    public ResultVO getAllCars(@RequestParam String user_id) {
        try {
            return new ResultVO(ResCode.SUCCESS,carServiceImpl.findByUid(user_id));
        }catch (Exception e){
            return new ResultVO(ResCode.ERROR,e);
        }
    }
    //通过车辆id查找车辆
    @GetMapping("/findCarId")
    public ResultVO findCarId(@RequestParam String car_id) {
        try {
            return new ResultVO(ResCode.SUCCESS,carServiceImpl.findByCarId(car_id));
        }catch (Exception e){
            return new ResultVO(ResCode.ERROR,e);
        }
    }
    //通过车辆id删除车辆
    @GetMapping("/delete")
    public ResultVO delete(@RequestParam String car_id){
        try {
            carServiceImpl.delete(car_id);
            return new ResultVO(ResCode.SUCCESS,"删除车辆成功");
        }catch (Exception e){
            return new ResultVO(ResCode.ERROR,e);
        }
    }
    //修改车辆可用状态
    @PostMapping("/updateCarStatu")
    public ResultVO delete(@RequestBody Map<String,String> car_info){
        try {
            carServiceImpl.updateStatu(car_info);
            return new ResultVO(ResCode.SUCCESS,"修改车辆状态成功");
        }catch (Exception e){
            return new ResultVO(ResCode.ERROR,e);
        }
    }
    //查找我租用的车辆
    @GetMapping("/findPerson")
    public ResultVO findPerson(@RequestParam String person_id){
        try {
            return new ResultVO(ResCode.SUCCESS,carServiceImpl.findPerson(person_id));
        }catch (Exception e){
            return new ResultVO(ResCode.ERROR,e);
        }
    }
    //归还车辆
    @GetMapping("/returnCar")
    public ResultVO returnCar(@RequestParam String car_id){
        try {
            return new ResultVO(ResCode.SUCCESS,carServiceImpl.returnCar(car_id));
        }catch (Exception e){
            return new ResultVO(ResCode.ERROR,e);
        }
    }
}
